package com.qkl.xiatang.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qkl.xiatang.app.dto.BusinessDTO;
import com.qkl.xiatang.app.entity.feed.AbnormalEntity;
import com.qkl.xiatang.app.mapper.AbnormalMapper;
import com.qkl.xiatang.app.utils.AppSessionUtils;
import com.qkl.xiatang.app.utils.AppUtils;
import com.qkl.xiatang.app.utils.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class AbnormalService {

    @Autowired
    private AbnormalMapper abnormalMapper;

    /**
     * 异常汇报
     * @param pondId
     * @param reportMemo
     * @param abnorImageList
     * @return
     */
    public BusinessDTO<Object> reportAbnormal(String pondId, String reportMemo, List<String> abnorImageList ){
        if(StringUtils.isEmpty(pondId)){
            return BusinessDTO.failBusiness("异常汇报请选择塘号");
        }
        String companyId = AppSessionUtils.getCompanyId();
        Date reportDate = AppUtils.todayYyyyMMdd();
        QueryWrapper queryWrapper = new QueryWrapper<AbnormalEntity>().eq("company_id",companyId).eq("pond_id",pondId).eq("report_date", reportDate).orderByDesc("create_time").last("limit 1");
        AbnormalEntity abnormalEntity = abnormalMapper.selectOne(queryWrapper);
        String abnormalId = "";
        if(null == abnormalEntity){
            abnormalId = AppUtils.genIdStr();
            AbnormalEntity addEntity = new AbnormalEntity();
            addEntity.setId(abnormalId);
            addEntity.setCompanyId(companyId);
            addEntity.setPondId(pondId);
            if(StringUtils.isNotEmpty(reportMemo)) {
                addEntity.setReportMemo(reportMemo);
            }
            if(ObjectUtils.isNotEmpty(abnorImageList)) {
                addEntity.setReportImages(JSON.toJSONString(abnorImageList));
            }
            addEntity.setReportDate(reportDate);
            abnormalMapper.insert(addEntity);
        }else {
            abnormalId = abnormalEntity.getId();
            AbnormalEntity updEntity = new AbnormalEntity();
            updEntity.setId(abnormalId);
            String line = Constants.ENTER_LINE;
            if(StringUtils.isNotEmpty(reportMemo)){
                String memo = abnormalEntity.getReportMemo();
                if(StringUtils.isNotEmpty(memo)){
                    reportMemo = memo + line + reportMemo;
                }
                updEntity.setReportMemo(reportMemo);;
            }
            if(ObjectUtils.isNotEmpty(abnorImageList)){
                List<String> list = new ArrayList<>();
                String images = abnormalEntity.getReportImages();
                if(StringUtils.isNotEmpty(images)){
                    list.addAll(JSON.parseArray(images,String.class));
                }
                list.addAll(abnorImageList);
                updEntity.setReportImages(JSON.toJSONString(list));
            }
            abnormalMapper.updateById(updEntity);
        }
        Map<String,String> map = new HashMap<>();
        map.put("abnormalId",abnormalId);
        return BusinessDTO.sucessBusiness(map,"异常汇报");
    }
}
